package com.zzh.lib.recorder;

import android.app.Application;
import android.util.DisplayMetrics;

import com.danikula.videocache.HttpProxyCacheServer;
import com.zzh.lib.recorder.def.IRecorder;
import com.zzh.lib.recorder.def.OnExceptionCallback;
import com.zzh.lib.recorder.def.OnRecorderCallback;
import com.zzh.lib.recorder.def.OnStateChangeCallback;
import com.zzh.lib.recorder.mp3.Mp3Recorder;

import java.io.File;

/**
 * Created by ZZH on 2021/11/28.
 *
 * @Date: 2021/11/28
 * @Email: zzh_hz@126.com
 * @QQ: 1299234582
 * @Author: zzh
 * @Description: 存储变量
 */
public class HRecorder {
    public static boolean DEBUG = false;
    Application mApplication;

    public static int screenWidth;
    public static int screenHeight;

    private static HRecorder mInstance;
    private OnStateChangeCallback mOnStateChangeCallback;
    private OnExceptionCallback mOnExceptionCallback;
    private OnRecorderCallback mOnRecorderCallback;
    private HMediaRecorderParams mRecorderParams;
    private AudioFormat mAudioFormat = AudioFormat.AAC;
    private IRecorder mRecorder;

    /**
     * 录制aac文件，pcm格式，amr格式时可以设置回调
     *
     * @param onStateChangeCallback
     * @return
     */
    public HRecorder setOnStateChangeCallback(OnStateChangeCallback onStateChangeCallback) {
        mOnStateChangeCallback = onStateChangeCallback;
        return this;
    }

    /**
     * 录制aac文件，pcm格式，amr格式时可以设置回调
     *
     * @param onExceptionCallback
     * @return
     */
    public HRecorder setOnExceptionCallback(OnExceptionCallback onExceptionCallback) {
        mOnExceptionCallback = onExceptionCallback;
        return this;
    }

    /**
     * 录制aac文件，pcm格式，amr格式时可以设置回调
     *
     * @param onRecorderCallback
     * @return
     */
    public HRecorder setOnRecorderCallback(OnRecorderCallback onRecorderCallback) {
        mOnRecorderCallback = onRecorderCallback;
        return this;
    }


    public HMediaRecorderParams getRecorderParams() {
        return mRecorderParams;
    }

    public HRecorder setRecorderParams(HMediaRecorderParams recorderParams) {
        mRecorderParams = recorderParams;
        return this;
    }

    public Application getContext() {
        if (mApplication == null) {
            throw new NullPointerException("mApplication == null");
        }
        return mApplication;
    }

    public void registerApplication(Application application) {
        mApplication = application;
        DisplayMetrics mDisplayMetrics = HRecorder.getApplication().getResources().getDisplayMetrics();
        screenWidth = mDisplayMetrics.widthPixels;
        screenHeight = mDisplayMetrics.heightPixels;
    }

    public static HRecorder getInstance() {
        if (mInstance == null) {
            synchronized (HRecorder.class) {
                if (mInstance == null) {
                    return mInstance = new HRecorder();
                }
            }
        }
        return mInstance;
    }


    public static void init(Application app) {
        getInstance().registerApplication(app);
    }

    public static Application getApplication() {
        return HRecorder.getInstance().getContext();
    }

    //=====================================================缓存区
    private HttpProxyCacheServer proxy;

    public static HttpProxyCacheServer getProxy() {
        HRecorder app = getInstance();
        return app.proxy == null ? (app.proxy = app.newProxy()) : app.proxy;
    }

    private HttpProxyCacheServer newProxy() {
        return new HttpProxyCacheServer(HRecorder.getApplication());
    }

    public OnStateChangeCallback getOnStateChangeCallback() {
        return mOnStateChangeCallback;
    }

    public OnExceptionCallback getOnExceptionCallback() {
        return mOnExceptionCallback;
    }

    public OnRecorderCallback getOnRecorderCallback() {
        return mOnRecorderCallback;
    }

    public AudioFormat getAudioFormat() {
        return mAudioFormat;
    }

    public HRecorder setAudioFormat(AudioFormat audioFormat) {
        mAudioFormat = audioFormat;
        return this;
    }

    public IRecorder init() {
        if (mAudioFormat == AudioFormat.MP3) {
            return mRecorder = initMp3Recorder();
        } else {
            return mRecorder = initMRecorder();
        }
    }

    /**
     * 开始录制
     */
    public void start() throws Exception {
        release();
        init();
        mRecorder.start();
    }

    private IRecorder initMRecorder() {
        HMediaRecorder hMediaRecorder = new HMediaRecorder();
        hMediaRecorder.setRecorderParams(mRecorderParams);
        hMediaRecorder.init(getContext());
        return hMediaRecorder;
    }

    private IRecorder initMp3Recorder() {
        Mp3Recorder mp3Recorder = new Mp3Recorder();
        return mp3Recorder;
    }

    /**
     * 停止录制
     */
    public void stop() {
        if (mRecorder != null) {
            mRecorder.stop();
        }
    }

    public HMediaRecorder.State getState() {
        if (mRecorder != null) {
            return mRecorder.getState();
        }
        return HMediaRecorder.State.Idle;
    }

    /**
     * 释放资源
     */
    public void release() {
        if (mRecorder != null) {
            mRecorder.release();
            mRecorder = null;
        }
    }
}
